<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;材料管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form">
        <el-row style="vertical-align:bottom;">
          <el-col :span="1">&nbsp;</el-col>
          <el-col :span="8" style="display:inline-block;">
            <el-form-item>
              <span style="font-size: 18px;float: left">搜索材料&nbsp;</span>
              <el-input v-model="inputName" placeholder="请输入材料名称" style="width: 500px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="material.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="registrationTime" label="登记时间" width="200px"></el-table-column>
        <el-table-column prop="name" label="材料名称" width="200px"></el-table-column>
        <el-table-column prop="unitIn" label="入库单位" width="200px"></el-table-column>
        <el-table-column prop="countIn" label="入库数量" width="200px"></el-table-column>
        <el-table-column prop="resIn" label="入库负责人" width="200px"></el-table-column>
        <el-table-column prop="unitOut" label="出库单位" width="200px"></el-table-column>
        <el-table-column prop="countOut" label="出库数量" width="200px"></el-table-column>
        <el-table-column prop="resOut" label="出库负责人" width="200px"></el-table-column>
        <el-table-column prop="count" label="现存数量" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                v-if="display==0"
                @click="handleEdit(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="material.length">
        </el-pagination>
      </div>
    </div>
    <!--修改材料弹出的窗口-->
    <el-dialog title="编辑材料"
               :visible.sync="dialogFormVisible"
               width="80%"
               append-to-body>
      <el-form :model="materialForm" :rules="rules" ref="materialForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="登记时间:" :label-width="formLabelWidth" prop="registrationTime">
              <el-date-picker
                  v-model="materialForm.registrationTime"
                  type="date"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  placeholder="选择登记时间"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="材料名称:" prop="name" :label-width="formLabelWidth">
              <el-input v-model="materialForm.name" placeholder="请输入材料名称" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库单位:" prop="unitIn" :label-width="formLabelWidth">
              <el-input v-model="materialForm.unitIn" placeholder="输入入库单位" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库数量:" prop="countIn" :label-width="formLabelWidth">
              <el-input v-model="materialForm.countIn" placeholder="输入入库数量" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库负责人:" prop="resIn" :label-width="formLabelWidth">
              <el-input v-model="materialForm.resIn" placeholder="输入入库负责人" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出库单位:" prop="unitOut" :label-width="formLabelWidth">
              <el-input v-model="materialForm.unitOut" placeholder="输入出库单位"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出库数量:" prop="countOut" :label-width="formLabelWidth">
              <el-input v-model="materialForm.countOut" placeholder="输入出库数量" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出库负责人:" prop="resOut" :label-width="formLabelWidth">
              <el-input v-model="materialForm.resOut" placeholder="输入出库负责人" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="现存数量:" prop="count" :label-width="formLabelWidth">
              <el-input v-model="materialForm.count" placeholder="输入现存数量" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item style="float: right">
              <el-button type="primary" @click="compute" style="background-color: white;border-color: gray">
                <i class="el-icon-thumb" style="color:gray;font-size: 100%">&nbsp;&nbsp;计算</i>
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" style="color: white" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" style="color: white" @click="submitAdminFrom()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formLabelWidth: '120px',
      dialogFormVisible: false,
      inputName: '',
      display: 0,
      materialN: [],
      material: [],
      materialForm: {
        registrationTime: '',
        name: '',
        unitIn: '',
        countIn: '',
        resIn: '',
        unitOut: '',
        countOut: '',
        resOut: '',
        count: '',
      },
      pageSize: 10,
      currentPage: 1,
      rules: {
        registrationTime: [
          {required: true, message: "请选择登记时间", trigger: 'change'}
        ],
        name: [
          {required: true, message: "请输入材料名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        unitIn: [
          {required: true, message: "请输入入库单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        countIn: [
          {required: true, message: "请输入入库数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        resIn: [
          {required: true, message: "请输入入库负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        unitOut: [
          {required: true, message: "请输入出库单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        countOut: [
          {required: true, message: "请输入出库数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        resOut: [
          {required: true, message: "请输入出库负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        count: [
          {required: true, message: "请输入现存数量", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
      }

    }
  },
  methods: {
    /*加载表格*/
    loadMaterialNList() {
      let url = 'http://localhost:9080/produceMaterials';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.material = responseBody.data;
          this.materialN = this.material;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    /*查询材料明细*/
    searchName(keywords) {
      return this.material.filter(item => {
        if (item.name.includes(keywords)) {
          return item
        }
      })
    },
    /*弹出修改的窗口*/
    handleEdit(material) {
      let url = 'http://localhost:9080/produceMaterials/' + material.id;
      console.log('id=' + material.id);
      console.log('url' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.materialForm = responseBody.data;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadMaterialNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*修改材料的窗口*/
    submitAdminFrom() {
      let url = 'http://localhost:9080/produceMaterials/' + this.materialForm.id + '/update';
      console.log('url' + url);
      let formData = this.qs.stringify(this.materialForm);
      console.log('formData=' + formData)

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            type: 'success',
            message: '修改材料成功!'
          });
          this.dialogFormVisible = false;
          this.loadMaterialNList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadMaterialNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*弹出删除材料窗口*/
    openDeleteConfirm(material) {
      let title = '提示';
      let message = '此操作将永久删除【' + material.name + '】材料, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(material);
      }).catch(() => {
      });
    },
    handleDelete(material) {
      let url = 'http://localhost:9080/produceMaterials/' + material.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除属性【' + material.name + '】成功！',
            type: 'success'
          });
          this.loadMaterialNList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadMaterialNList();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
    compute() {
      let count =
          parseInt(this.materialForm.countIn) -
          parseInt(this.materialForm.countOut);
      this.$set(this.materialForm, 'count', count);
    },
  },
  mounted() {
    this.loadMaterialNList();
  },
  watch: {
    inputName: function (val) {
      if (val == '') {
        this.material = this.materialN
      } else {
        this.material = this.searchName(val)
      }
    }
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.material_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>